<?php
//wget http://www.rpmfind.net/linux/centos/6.10/os/x86_64/Packages/ImageMagick-6.7.2.7-6.el6.x86_64.rpm
//yum install -y ImageMagick-6.7.2.7-6.el6.x86_64.rpm

//wget http://www.imagemagick.org/download/ImageMagick-6.9.11-2.tar.gz
//tar xvf  ImageMagick-6.9.11-2.tar.gz
//cd ImageMagick-6.9.11-2
//./configure

function get_image() {
	$image = isset($_GET['image']) ? $_GET['image'] : '';
	if (!$image) {
		return ['code' => -1];
	}
	$arr = explode('!', $image);
	if (count($arr) == 2) {
		return ['code' => 1, 'path' => $arr[0], 'w' => $arr[1]];
	}
	return ['code' => -1];
}

function getImagetype($filename) {
	$file = fopen($filename, 'rb');
	$bin = fread($file, 2); //只读2字节
	fclose($file);
	$strInfo = @unpack('C2chars', $bin);
	$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
	// dd($typeCode);
	$fileType = '';
	switch ($typeCode) {
	case 255216:
		$fileType = 'jpg';
		break;
	case 7173:
		$fileType = 'gif';
		break;
	case 6677:
		$fileType = 'bmp';
		break;
	case 13780:
		$fileType = 'png';
		break;
	default:
		$fileType = '只能上传图片类型格式';
	}
	return $fileType;

}

function out_put($path) {
	$type = getImagetype($path);
	header("Content-Type:image/{$type}");
	echo file_get_contents($path);
	exit;
}

function image_thumb($origin_path, $thumb_path, $w, $type = null) {
	if (!$type) {
		$type = array_pop(explode('.', $origin_path));
	}
	$do_with_php = true;
	if ($do_with_php) {
		image_thumb_php($origin_path, $thumb_path, $w, $type);
		exit();
	} else {
		if (strtolower($type) == 'gif') {
			$cmd = "convert {$origin_path} -fuzz 15% -layers Optimize {$thumb_path} ";
		} else {
			$cmd = "convert -resize {$w}x {$origin_path} {$thumb_path} ";
		}
		system($cmd);
	}
}

function image_thumb_php($origin_path, $thumb_path, $w, $type = null) {
	if ($type == null) {
		$type = getImagetype($origin_path);
	}
	if ($type == 'png') {
		$im = imagecreatefrompng($origin_path); //創建一張圖片
	} elseif ($type == 'jpg') {
		$im = imagecreatefromjpeg($origin_path);
	} elseif ($type == 'bmp') {
		$im = imagecreatefromwbmp($origin_path);
	} elseif ($type == 'gif') {
		$im = imagecreatefromgif($origin_path);
	} else {
//
		//TODO:不能压缩的原图过去
		echo "err";
		exit();
	}

	$size = getimagesize($origin_path); //得到
	$real_w = $size[0];
	$real_h = $size[1];
	$h = $w * $real_h / $real_w;
	$t_im = imagecreatetruecolor($w, $h);
	imagealphablending($t_im, false);
	imagesavealpha($t_im, true);
	///	// 合成图 原图  合成图x 合成图y 原图x 原图y 合成w 合成h 原图w 原图h
	imageCopyreSampled($t_im, $im, 0, 0, 0, 0, $w, $h, $real_w, $real_h);

	if ($type == 'png') {
		imagepng($t_im, $thumb_path); //文字生成的图片
	} elseif ($type == 'jpg') {
		imagejpeg($t_im, $thumb_path); //文字生成的图片
	} elseif ($type == 'bmp') {
		imagewbmp($t_im, $thumb_path); //文字生成的图片
	} elseif ($type == 'gif') {
		imagegif($t_im, $thumb_path); //文字生成的图片
	} else {
		//TODO:不能压缩的原图过去
		echo "err";
		exit();
	}

	// header("Content-type:image/png");
	// imagepng($t_im, $thumb_path); //文字生成的图片
	out_put($thumb_path);
	exit();

	// file_put_contents($thumb_path, $t_im);
	// out_put($thumb_path);
	// exit();
}

$info = get_image();
if ($info['code'] > 0) {
	$w = $info['w'];
	if (!in_array($w, [100, 200, 300, 400, 500, 600, 700, 800, 900])) {
		$w = 300;
	}
	$origin_path = '/usr/share/nginx/www/image_server/Upload' . $info['path'];
	$thumb_path = '/usr/share/nginx/www/image_server/Upload/thumb' . $info['path'] . '!' . $w;
	if (file_exists($thumb_path)) {
		out_put($thumb_path);
		exit();
	}
	if (file_exists($origin_path)) {
		$type = getImagetype($origin_path);
		image_thumb($origin_path, $thumb_path, $w, $type);
		if (file_exists($thumb_path)) {
			out_put($thumb_path);
			exit();
		}
	} else {
		Header("HTTP/1.1 404 Not Found");
		exit();
	}
}
Header("HTTP/1.1 404 Not Found");
exit();

// echo phpinfo();
